package com.xinggq.pay.service.controller;

import com.xinggq.common.CommonResult;
import com.xinggq.pay.api.dto.YseCommonPayPespDTO;
import com.xinggq.pay.api.dto.YseCommonPayReqDTO;
import com.xinggq.pay.api.dto.YseCommonRefundReqDTO;
import com.xinggq.pay.api.dto.YseCommonRefundRespDTO;
import com.xinggq.pay.service.pay.channel.yse.other.refund.YseRefundServiceImpl;
import com.xinggq.pay.service.pay.faced.YseFacedService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 银盛支付API控制器
 * @author xinggq
 * @since 2025-01-20
 */
@Tag(name = "银盛支付")
@RestController
@RequestMapping("/api/yse")
@Slf4j
@RequiredArgsConstructor
public class YsePayController {

    private final YseFacedService yseFacedService;
    private final YseRefundServiceImpl refundService;

    @Operation(summary = "统一支付")
    @PostMapping("/pay")
    public CommonResult<YseCommonPayPespDTO<?>> pay(
            @Parameter(description = "支付请求参数")
            @RequestBody @Validated YseCommonPayReqDTO payReqDTO) {
        log.info("收到统一支付请求，商户订单号：{}", payReqDTO.getMerchantOutTradeNo());
        YseCommonPayPespDTO<?> result = yseFacedService.pay(payReqDTO);
        log.info("统一支付处理完成，支付平台订单号：{}", result.getTradeNo());
        return CommonResult.ok(result);
    }

    @Operation(summary = "退款")
    @PostMapping("/refund")
    public CommonResult<YseCommonRefundRespDTO> refund(
            @Parameter(description = "退款请求参数")
            @RequestBody @Validated YseCommonRefundReqDTO refundReq) {
        log.info("收到退款请求，商户订单号：{}", refundReq.getMerchantOutTradeNo());
        YseCommonRefundRespDTO refund = refundService.refund(refundReq);
        log.info("退款请求处理完成，支付平台退款单订单号：{}", refund.getTradeNo());
        return CommonResult.ok(refund);
    }
}